#!/bin/sh
# Generate a rss feed from a file containing new pages
# usage : swx_rss SRCDIR > feed.xml

# Load config file
if [ ! -f $PWD/swx.conf ]; then
	echo "Cannot find swx.conf in current directory"
	exit 1
fi
. $PWD/swx.conf

if [ ! -f "$LOG" ]; then
    echo "Can't find the News file"
    exit 1
fi

WORKTMP=$(mktemp)
# keep MAX news
uniq -u "$LOG" | tail -n $MAX > "$WORKTMP"
mv "$WORKTMP" "$LOG"

# new entry first
tail -r "$LOG" | sed -e "s;$1;;g" > $WORKTMP

echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
echo "<rss version=\"2.0\" xmlns:atom=\"http://www.w3.org/2005/Atom\">"
echo "<channel>"
echo "   <title>$TITLE</title>"
echo "   <description><![CDATA[$SUBTITLE]]></description>"
echo "   <language>fr</language>"

while read line 
do
    #LINK="/$(echo $PATH | cut -d'/' -f2- | sed -e "s,$EXT$,.html,g")"
    #echo $PATH
    echo "      <item>"
    NEWTITLE="$(basename $line | sed -e "s,$EXT$,,g")"
    if [ "$NEWTITLE" = "index" ]; then
        NEWTITLE="$(basename $(dirname $line))"
    fi
    echo "      <title>$NEWTITLE</title>"
    echo "      <link>/$(echo $line | cut -d'/' -f2- | sed -e "s,$EXT$,.html,g")</link>"
    echo "      <guid>/$(echo $line | cut -d'/' -f2- | sed -e "s,$EXT$,.html,g")</guid>"
    DATE=$(ls -l "$1/$line" | awk '{print $6"-"$7"-"$8}')
    echo "      <pubdate>$DATE</pubdate>"
    echo  "      <description><![CDATA[$($CONVERTER $1/$line | sed 's/\\/\\\\/g')]]></description>"
    # uncomment for plaint text description
    #echo  "      <description><![CDATA[$(sed 's/\\/\\\\/g' $line)]]></description>"
    echo "\n      </item>"
done < $WORKTMP

rm $WORKTMP

echo "</channel>"
echo "</rss>"

 


